<template>
  <el-card>
    <el-table
      :data="PermisstionArr"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
    >
      <el-table-column label="名称" prop="name"></el-table-column>
      <el-table-column label="权限值" prop="code"></el-table-column>
      <el-table-column label="修改时间" prop="updateTime"></el-table-column>
      <el-table-column label="操作" width="300">
        <template #="{ row, $index }">
          <el-button
            type="primary"
            @click="onAdd(row)"
            size="small"
            :disabled="row.level == 4 ? true : false"
          >
            {{ row.level == 3 ? '添加按钮' : '添加菜单' }}
          </el-button>
          <el-button
            type="primary"
            @click="onEdit(row)"
            size="small"
            :disabled="row.level == 1 ? true : false"
          >
            编辑
          </el-button>
          <el-popconfirm
            :title="`你确定要删除${row.name}?`"
            width="260px"
            @confirm="onDelete(row.id)"
          >
            <template #reference>
              <el-button
                type="danger"
                size="small"
                :disabled="row.level == 1 ? true : false"
              >
                删除
              </el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <AddMenu
      v-if="showAddDlg"
      :showDlg="showAddDlg"
      :row="menuData"
      @closeDlg="showAddDlg = false"
      @saveDlg="saveDlg"
    ></AddMenu>
  </el-card>
</template>

<script setup lang="ts">
import { ref, onMounted, reactive } from 'vue'
import { reqAllPermisstion, reqRemoveMenu } from '@/api/acl/menu/menu'
import AddMenu from './addMenu.vue'
import type {
  MenuParams,
  PermisstionResponseData,
  PermisstionList,
  Permisstion,
} from '@/api/acl/menu/type'
import { ElMessage } from 'element-plus'
let showAddDlg = ref<boolean>(false)
let PermisstionArr = ref<PermisstionList>([])
let menuData = reactive<MenuParams>({
  code: '',
  level: 0,
  name: '',
  pid: 0,
})
//组件挂载完毕
onMounted(() => {
  getHasPermisstion()
})
//获取菜单数据的方法
const getHasPermisstion = async () => {
  let result: PermisstionResponseData = await reqAllPermisstion()
  if (result.code == 200) {
    PermisstionArr.value = result.data
  }
}

//添加菜单按钮的回调
const onAdd = (row: Permisstion) => {
  console.log('row: ', row)
  Object.assign(menuData, {
    id: 0,
    code: '',
    level: 0,
    name: '',
    pid: 0,
  })
  menuData.level = row.level + 1
  showAddDlg.value = true
  menuData.pid = row.id as number
}
//编辑已有的菜单
const onEdit = (row: Permisstion) => {
  showAddDlg.value = true

  Object.assign(menuData, row)
}

const saveDlg = () => {
  showAddDlg.value = false
  getHasPermisstion()
}
const onDelete = async (id: number) => {
  let result = await reqRemoveMenu(id)
  if (result.code == 200) {
    ElMessage({ type: 'success', message: '删除成功' })
    getHasPermisstion()
  } else {
    ElMessage({ type: 'success', message: '删除失败' })
  }
}
</script>

<style scoped></style>
